package com.yequan;

import com.yequan.compare.Dog;
import redis.clients.jedis.Jedis;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: yq
 * @Date: 2018/8/6 19:58
 * @Description:
 */
public class Main {
    public static void main(String[] args) {
//        System.out.println("web learn");
        Jedis jedis = new Jedis("47.95.254.54", 6379);
//        jedis.set("hello","world");
//        System.out.println(jedis.get("hello"));
//        List<String> values = jedis.mget("hello","key1");
//        for (String value : values) {
//            System.out.println(value);
//        }

//        long start = System.currentTimeMillis();
//        for (int i = 0; i < 1000; i++) {
//            jedis.hset("hashKey" + i, "field" + i, "value" + i);
//        }
//        long end = System.currentTimeMillis();
//        System.out.println("common: " + (end - start));
//        long pipelineStart = System.currentTimeMillis();
//        for (int i = 0; i < 100; i++) {
//            Pipeline pipelined = jedis.pipelined();
//            for (int j = i * 100; j < (i + 1) * 100; j++) {
//                pipelined.hset("hashKey_" + j, "field" + j, "value" + j);
//            }
//            pipelined.syncAndReturnAll();
//        }
//        long pipelineEnd = System.currentTimeMillis();
//        System.out.println("pipelineEnd: " + (pipelineEnd - pipelineStart));

        Dog tom = new Dog("tom", true, true);
        Dog tom2 = new Dog("tom", true, true);
        Dog jack = new Dog("jack", true, false);

        List<String>  fieldNames1 = compareContract(tom, tom2);
        if (fieldNames1==null) {
            System.out.println("对象内容一样");
        } else {
            System.out.println();
        }
        System.out.println(">>>>>>>>>>>>>>>>>>>");
        List<String>  fieldNames2 = compareContract(tom, jack);
        if (fieldNames2==null) {
            System.out.println("对象内容一样");
        } else {
            System.out.println("对象内容不一样");
        }

    }

    public static List<String> compareContract(Dog sign, Dog existSign) {
        List<String> fieldNames = new ArrayList<String>();
        try {
            Field[] fields = getAllFields(sign);
            for (int j = 0; j < fields.length; j++) {
                fields[j].setAccessible(true);
                // 字段值
                if (!fields[j].get(sign).equals(fields[j].get(existSign))) {
                    fieldNames.add(fields[j].getName());
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return fieldNames.size() > 0 ? fieldNames : null;
    }

    /**
     * 获取所有属性，包括父类
     *
     * @param object
     * @return
     */
    public static Field[] getAllFields(Object object) {
        Class clazz = object.getClass();
        List<Field> fieldList = new ArrayList<Field>();
        while (clazz != null) {
            fieldList.addAll(new ArrayList<Field>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        return fields;
    }


}
